package cn.ljy.uem.service.impl;

import cn.ljy.authority.dao.mapper.SysUserMapper;
import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyForbiddenException;
import cn.ljy.uem.dao.ProjectMapper;
import cn.ljy.uem.dao.ProjectMenberDailyMapper;
import cn.ljy.uem.dao.ProjectTeamMapper;
import cn.ljy.uem.model.ProjectMenberDaily;
import cn.ljy.uem.model.query.ProjectMenberDailyQuery;
import cn.ljy.uem.service.ProjectMenberDailyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;


/**
 * 工作日志业务实现类
 *
 * @author 小锋
 * @date 2023/01/26
 */
@Service
@Slf4j
public class IProjectMenberDailyService extends IBaseService<ProjectMenberDaily, ProjectMenberDailyQuery> implements ProjectMenberDailyService {

    @Autowired
    private ProjectMenberDailyMapper projectMenberDailyMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private ProjectTeamMapper projectTeamMapper;


    @Override
    protected void beforeGet(Serializable id) {
        if (id == null) {
            throw new MyForbiddenException("操作失败：请输入要查询的id");
        } else {
            if (projectMenberDailyMapper.findById(id) == null) {
                throw new MyForbiddenException("操作失败：查找不到指定记录");
            }
        }
    }

    @Override
    protected void beforeUpdate(ProjectMenberDaily projectMenberDaily) {
        if (projectMenberDaily != null) {
            if (projectMenberDaily.getId() == null) {
                throw new MyForbiddenException("操作失败：修改对象为空");
            }
        } else {
            throw new MyForbiddenException("操作失败：修改对象为空");
        }
    }

    @Override
    protected void beforeSave(ProjectMenberDaily projectMenberDaily) {
        if (projectMenberDaily != null) {
            //查找当前操作者是否存在
            if (projectMenberDaily.getAuthorId() != null) {
                if (sysUserMapper.findById(projectMenberDaily.getAuthorId()) == null) {
                    throw new MyForbiddenException("操作失败：用户外键查询记录为空");
                }
            } else {
                throw new MyForbiddenException("操作失败：请输入当前操作者(用户)id");
            }
            //查找所属于工程是否存在
            if (projectMenberDaily.getProjectId() != null) {
                if (projectMapper.findById(projectMenberDaily.getProjectId()) == null) {
                    throw new MyForbiddenException("操作失败：所属工程查询记录为空");
                }
            } else {
                throw new MyForbiddenException("操作失败：请输入所属工程id");
            }
            //查询项目组是否存在
            if (projectMenberDaily.getTeamId() != null) {
                if (projectTeamMapper.findById(projectMenberDaily.getTeamId()) == null) {
                    throw new MyForbiddenException("操作失败：所属项目组查询记录为空");
                }
            } else {
                throw new MyForbiddenException("操作失败：请输入所属项目组id");
            }
        } else {
            throw new MyForbiddenException("操作失败：添加对象为空");
        }
    }

    @Override
    protected void beforeDelete(Serializable[] ids) {
        if (ids.length <= 0) {
            throw new MyForbiddenException("操作失败：请输入您要删除的id");
        }
    }
}
